package usejava.threadpool;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class MyRejectedExecutionHandler implements RejectedExecutionHandler {

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        System.out.println("Thread pool rejected a task: " + r.toString());
        if (!executor.isShutdown()) {
            try {
                // 尝试直接运行被拒绝的任务
                executor.getQueue().put(r);
            } catch (InterruptedException ie) {
                // 重新设置中断标记
                Thread.currentThread().interrupt();
            }
        }
    }
}
